/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.utility;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author student
 */
public class DBUtil {

    private static Connection connection = null;

    /**
     * @param dbType
     */
    public static Connection getConnection(DBType dbType) throws SQLException {
        if (connection != null) {
            return connection;
        } else {
            try {
                // Get db.properties files
                Properties prop = new Properties();
                InputStream inputStream = DBUtil.class.getClassLoader().getResourceAsStream("com/aptech/utility/db.properties");
//                InputStream inputStream = new FileInputStream(new File("com/aptech/utility/db.properties"));
                prop.load(inputStream);

                // get connection string for every member
                String phthangUrl = prop.getProperty("phthangUrl");
                String myConnectionUrl = prop.getProperty("myConnection");
                String tdkhoaUrl = prop.getProperty("tdkhoaUrl");
                String nhanhUrl = prop.getProperty("nhanhUrl");
                String dqthanhUrl = prop.getProperty("dqthanhUrl");
                
                
                System.out.println("Thang " + phthangUrl);
                System.out.println("Khoa " + tdkhoaUrl);
                System.out.println("Nhanh " + nhanhUrl);
                System.out.println("Thanh " + dqthanhUrl);
                System.out.println("My connection " + myConnectionUrl);
                switch (dbType) {
                    case phthang:
                        return DriverManager.getConnection(phthangUrl);
                    case tdkhoa:
                        return DriverManager.getConnection(tdkhoaUrl);
                    case nhanh:
                        return DriverManager.getConnection(nhanhUrl);
                    case dqthanh:
                        return DriverManager.getConnection(dqthanhUrl);
                    case myConnection:
                        return DriverManager.getConnection(myConnectionUrl);
                    default:
                        inputStream.close();
                        return null;
                }
            } catch (IOException ex) {
                Logger.getLogger(DBUtil.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
        return connection;
    }
}
